﻿namespace WMS6.Web.Customs
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CustomsServiceRef;
    using WMS6.Web.UserControls;

    public class ExportDeclarationsList : BasePage
    {
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F79822;
        protected GridView gvExportDeclarations;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblExportDeclarations;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2ExportDeclarationsList(ExportDeclarationsQueryEntity exportDeclarationsQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(exportDeclarationsQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = CustomsServiceClientProxy.GetInstance().GetEXDeclarationsInfoByQueryEntity(exportDeclarationsQuery);
            base.BindData2GridView(this.gvExportDeclarations, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvExportDeclarations, "DECLARATIONS_ID");
            List<string> status = base.GetSelectedIDsInGridView(this.gvExportDeclarations, "STATUS");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = "请至少选择一项进行删除操作";
            }
            else
            {
                for (int i = 0; status.Count > i; i++)
                {
                    if (status[i].ToUpper() != "NEW")
                    {
                        this.lblMsg.Text = "报关单号" + selectedID[i] + "状态不等于新建无法删除!";
                        return;
                    }
                }
                try
                {
                    this.lblMsg.Text = "删除入区报关单号：" + CustomsServiceClientProxy.GetInstance().DeleteEXDeclarationsInfo(selectedID);
                    ExportDeclarationsQueryEntity exportDeclarationsQuery = this.GetExportDeclarationsQueryEntity();
                    this.BindData2ExportDeclarationsList(exportDeclarationsQuery);
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvExportDeclarations, "DECLARATIONS_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = "请选择一项进行编辑";
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = "只能选择一项进行编辑";
            }
            else
            {
                base.Response.Redirect(string.Format("ExportDeclarations.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            ExportDeclarationsQueryEntity exportDeclarationsQuery = this.GetExportDeclarationsQueryEntity();
            exportDeclarationsQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = exportDeclarationsQuery.CurrentPage;
            exportDeclarationsQuery.CurrentPage = "0";
            this.gvExportDeclarations.Columns[1].Visible = false;
            this.gvExportDeclarations.AllowSorting = false;
            this.BindData2ExportDeclarationsList(exportDeclarationsQuery);
            this.hdnActionID.Value = "";
            this.gvExportDeclarations.ShowFooter = false;
            exportDeclarationsQuery.IsGetAll = false;
            exportDeclarationsQuery.CurrentPage = currentPageIndex;
            this.SetExportDeclarationsQueryEntity(exportDeclarationsQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("ExportDeclarations.xls", this.gvExportDeclarations);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            ExportDeclarationsQueryEntity exportDeclarationsQuery = this.GetExportDeclarationsQueryEntity();
            exportDeclarationsQuery.CurrentPage = "0";
            this.BindData2ExportDeclarationsList(exportDeclarationsQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            ExportDeclarationsQueryEntity exportDeclarationsQuery = this.GetExportDeclarationsQueryEntity();
            exportDeclarationsQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2ExportDeclarationsList(exportDeclarationsQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            ExportDeclarationsQueryEntity exportDeclarationsQuery = this.GetExportDeclarationsQueryEntity();
            exportDeclarationsQuery.DeclarationsID = "";
            exportDeclarationsQuery.DeclarationsType = "";
            exportDeclarationsQuery.OwnerID = "";
            exportDeclarationsQuery.OrderID = "";
            exportDeclarationsQuery.DeclarationsDate = "";
            exportDeclarationsQuery.Status = "";
            this.BindData2ExportDeclarationsList(exportDeclarationsQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            ExportDeclarationsQueryEntity exportDeclarationsQuery = this.GetExportDeclarationsQueryEntity();
            if (int.Parse(exportDeclarationsQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                exportDeclarationsQuery.CurrentPage = (int.Parse(exportDeclarationsQuery.CurrentPage) + 1).ToString();
                this.BindData2ExportDeclarationsList(exportDeclarationsQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            ExportDeclarationsQueryEntity exportDeclarationsQuery = this.GetExportDeclarationsQueryEntity();
            if (int.Parse(exportDeclarationsQuery.CurrentPage) > 0)
            {
                exportDeclarationsQuery.CurrentPage = (int.Parse(exportDeclarationsQuery.CurrentPage) - 1).ToString();
                this.BindData2ExportDeclarationsList(exportDeclarationsQuery);
            }
        }

        private ExportDeclarationsQueryEntity GetExportDeclarationsQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as ExportDeclarationsQueryEntity);
            }
            return (base.PageQueryEntitySession as ExportDeclarationsQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                ExportDeclarationsQueryEntity exportDeclarationsQuery = this.GetExportDeclarationsQueryEntity();
                exportDeclarationsQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2ExportDeclarationsList(exportDeclarationsQuery);
            }
        }

        protected void gvExportDeclarations_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F79822", this.gvExportDeclarations);
        }

        protected void gvExportDeclarations_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblRemark"));
            }
        }

        protected void gvExportDeclarations_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                ExportDeclarationsQueryEntity exportDeclarationsQuery = this.GetExportDeclarationsQueryEntity();
                exportDeclarationsQuery.SortField = e.SortExpression;
                if (exportDeclarationsQuery.SortDirection == "ASC")
                {
                    exportDeclarationsQuery.SortDirection = "DESC";
                }
                else if (exportDeclarationsQuery.SortDirection == "DESC")
                {
                    exportDeclarationsQuery.SortDirection = "ASC";
                }
                this.BindData2ExportDeclarationsList(exportDeclarationsQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            ExportDeclarationsQueryEntity exportDeclarationsQuery = new ExportDeclarationsQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    exportDeclarationsQuery.DeclarationsID = this.PreviousPage.DeclarationsID;
                    exportDeclarationsQuery.DeclarationsType = this.PreviousPage.DeclarationsType;
                    exportDeclarationsQuery.OwnerID = this.PreviousPage.OwnerID;
                    exportDeclarationsQuery.OrderID = this.PreviousPage.OrderID;
                    exportDeclarationsQuery.DeclarationsDate = this.PreviousPage.DeclarationsDate;
                    exportDeclarationsQuery.Status = this.PreviousPage.Status;
                }
                else
                {
                    exportDeclarationsQuery.DeclarationsID = "";
                    exportDeclarationsQuery.DeclarationsType = "";
                    exportDeclarationsQuery.OwnerID = "";
                    exportDeclarationsQuery.OrderID = "";
                    exportDeclarationsQuery.DeclarationsDate = "";
                    exportDeclarationsQuery.Status = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            exportDeclarationsQuery.IsGetAll = false;
            exportDeclarationsQuery.CurrentPage = "0";
            exportDeclarationsQuery.PageSize = this.gvExportDeclarations.PageSize.ToString();
            exportDeclarationsQuery.SortField = "DECLARATIONS_ID";
            exportDeclarationsQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvExportDeclarations.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetExportDeclarationsQueryEntity(exportDeclarationsQuery);
            this.BindData2ExportDeclarationsList(exportDeclarationsQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetExportDeclarationsQueryEntity(ExportDeclarationsQueryEntity exportDeclarationsQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = exportDeclarationsQuery;
            }
            else
            {
                base.PageQueryEntitySession = exportDeclarationsQuery;
            }
        }

        public ExportDeclarationsQuery PreviousPage
        {
            get
            {
                return (ExportDeclarationsQuery) base.PreviousPage;
            }
        }
    }
}

